package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.dto.UserLoginDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.entity.User;
import com.sky.exception.LoginFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.mapper.UserMapper;
import com.sky.properties.WeChatProperties;
import com.sky.service.ShoppingCartService;
import com.sky.service.UserService;
import com.sky.utils.HttpClientUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

/**
 * 套餐
 */
@Slf4j
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 加入购物车
     * @param shoppingCartDTO
     */
    @Override
    @Transactional
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
         ShoppingCart shoppingCart = ShoppingCart.builder().build();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        log.info("userId:{}",shoppingCart.getUserId());

        //判断购物车是否存在了
      List<ShoppingCart> list =  shoppingCartMapper.list(shoppingCart);
      if(list.size()>0&&list!=null){
         shoppingCart= list.get(0);
          shoppingCart.setNumber(shoppingCart.getNumber()+1);
          shoppingCartMapper.updateShoppingCart(shoppingCart);
      }else{
           Long dishId = shoppingCartDTO.getDishId();
           if(dishId!=null){
            Dish dish=dishMapper.getById(dishId);
            shoppingCart.setAmount(dish.getPrice());
            shoppingCart.setName(dish.getName());
            shoppingCart.setImage(dish.getImage());
           }else {
               Setmeal setmeal=setmealMapper.getById(shoppingCartDTO.getSetmealId());
               shoppingCart.setAmount(setmeal.getPrice());
               shoppingCart.setName(setmeal.getName());
               shoppingCart.setImage(setmeal.getImage());
           }
           shoppingCart.setCreateTime(LocalDateTime.now());
           shoppingCart.setNumber(1);
          shoppingCartMapper.insert(shoppingCart);
      }

    }

    /**
     * 购物车菜品或套餐数量减少
     * @param shoppingCartDTO
     */
    @Override
    @Transactional
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = ShoppingCart.builder().build();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        log.info("userId:{}",shoppingCart.getUserId());
        List<ShoppingCart> list =  shoppingCartMapper.list(shoppingCart);
             shoppingCart = list.get(0);
        if(shoppingCart.getNumber()>1){
            shoppingCart.setNumber(shoppingCart.getNumber()-1);
            shoppingCartMapper.updateShoppingCart(shoppingCart);
        }else {
            shoppingCartMapper.deleteShopping(shoppingCart);
        }

    }

    /**
     * 查看购物车
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
         ShoppingCart build = ShoppingCart.builder().userId(BaseContext.getCurrentId()).build();
        return shoppingCartMapper.list(build);

    }

    /**
     * 清空购物车
     */
    @Override
    public void clean() {
        shoppingCartMapper.deleteByUserId(BaseContext.getCurrentId());
    }
}
